//
//  TakeSelfieViewControllerViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 20/2/2023.
//

import UIKit
import AVFoundation
import HandyJSON
import CoreLocation
import FirebaseAnalytics

class FaceDetectSelfieVC: BaseViewController {
    var takeSelfeiClosure: ((UIImage, PhotoSource)->Void)?
    let customView = FaceDetectSelfieView()
    var naviTitle: String = ""//导航标题
    //
    var faceDetector = XZQRCode()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.faceDetector.prepareScan(self.view, scanSize: self.customView.cropRect.size, scanViewTopMargin: self.customView.cropRect.origin.y) { [weak self]  (image) in
            guard let strongSelf = self else {return}
            strongSelf.dismiss(animated: true,completion: {
                strongSelf.takeSelfeiClosure?(image,strongSelf.customView.selfiSource)
            })
        }
        //启动时前置蛇形头不可用
        if faceDetector.captureDevice == nil{
            self.customView.noAvailableDeviceLabel.isHidden = false
        }else{
            self.customView.noAvailableDeviceLabel.isHidden = true
            self.faceDetector.startScan()
        }
    }
    private func setupUI(){
        self.view.backgroundColor = .black
        self.view.addSubview(customView)
        //
        customView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.customView.setSelfieTipInfo(installerName: JobDetailModelHelper.shared.jobDetailModel.signatures.installer_full_name,checklistTitle: naviTitle)
        self.customView.swithCamaraBtn.addTarget(self, action: #selector(switchCamara), for: UIControl.Event.touchUpInside)
        self.customView.returnBtn.addTarget(self, action: #selector(returnBtnClick), for: UIControl.Event.touchUpInside)
        self.customView.takePhotoBtn.addTarget(self, action: #selector(takePhotoBtnClick), for: UIControl.Event.touchUpInside)
        self.customView.switchModeClosure = {[weak self] in
            guard let strongSelf = self else { return  }
            if strongSelf.customView.selfiSource == .autoRecognition{
                strongSelf.faceDetector.startAutoDetecting()
            }else{
                strongSelf.faceDetector.stopAutoDetecting()
            }
        }
    }
    //MARK: -切换前后摄像头
    @objc func switchCamara(){
        self.faceDetector.switchCamara()
    }
    @objc
    func returnBtnClick(){
        DispatchQueue.main.async {[weak self] in
            guard let strongSelf = self else { return  }
            strongSelf.faceDetector.stopScan()
        }
        self.dismiss(animated: true)
    }
    //MARK: - 手动拍照
    @objc func takePhotoBtnClick(){
        self.faceDetector.takePhoto()
    }
    //必须在 Info.plist 文件中添加 View controller-based status bar appearance ，并且必须设置为 YES
    override var prefersStatusBarHidden: Bool {
        return false
    }
    override func viewWillDisappear(_ animated: Bool) {
        self.faceDetector.stopScan()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
    deinit{
        self.faceDetector.stopScan()
        DebugLog("deinit")
    }
}
